package br.com.rglariguet.rgportal.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.rglariguet.rgportal.Cliente;
import br.com.rglariguet.rgportal.Endereco;
import br.com.rglariguet.rgportal.dao.ClienteDAO;
import br.com.rglariguet.rgportal.dao.EnderecoDAO;

@Controller
@RequestMapping("/admin/clientes")
public class ClienteController {
	
	@Autowired
	ClienteDAO clienteDAO;
	
	@Autowired
	EnderecoDAO enderecoDAO;
	
	@RequestMapping(value="/novo",   method = RequestMethod.GET)
	public ModelAndView novo(){
		ModelAndView mv = new ModelAndView("/admin/clientes/novoCliente");
		mv.addObject("statusList", preencherComboStatus());
		mv.addObject("cliente", new Cliente());
		return mv;
	}
	
	@RequestMapping(value="/salvar",  method = RequestMethod.POST)
	public String salvar(@Valid Cliente cliente, BindingResult br, ModelMap model){
		if (br.hasErrors()) {
			model.addAttribute("statusList", preencherComboStatus());
			return "/admin/clientes/novoCliente";
		}
		Endereco endereco = enderecoDAO.salvar(cliente.getEndereco());
		cliente.setEndereco(endereco);
		clienteDAO.salvar(cliente);
		return "redirect:/admin/clientes/listar";
	}
	
	@RequestMapping(value="/alterar",  method = RequestMethod.POST)
	public ModelAndView alterar(Cliente cliente){
		clienteDAO.alterar(cliente);
		ModelAndView mv = new ModelAndView("redirect:/admin/clientes/listar");
		return mv;
	}
	
	@RequestMapping(value="/editar/{id}",  method = RequestMethod.GET)
	public ModelAndView editar(@PathVariable Long id){
		Cliente cliente = clienteDAO.buscarPorId(id);
		ModelAndView mv = new ModelAndView("/admin/clientes/editarCliente");
		mv.addObject(cliente);
		mv.addObject("statusList", preencherComboStatus());
		return mv;
	}
	
	@RequestMapping(value="/excluir/{id}", method=RequestMethod.GET)	
	public String excluir(@PathVariable Long id) {
		Cliente cliente = clienteDAO.buscarPorId(id);
		clienteDAO.excluir(cliente);
		return "redirect:/admin/clientes/listar";
	}
	
	@RequestMapping("/listar")
	public String listarClientes(ModelMap model, Cliente cliente){
		List<Cliente> clientes = clienteDAO.listarTodos();
		model.addAttribute("clientes", clientes);
		return "admin/clientes/listaClientes";
	}
	
	protected Map<String, String> preencherComboStatus() {
		Map<String,String> status = new LinkedHashMap<String,String>();
		status.put("A", "Ativo");
		status.put("I", "Inativo");
		status.put("P", "Prospect");
		return status;
	}
}
